package com.mrazola;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;

public class Pod {

	public static final double DEFAULT_MOVEMENT_SPEED = 256; 	// (px/s)

	private Space space;
	private Sprite sprite;
	
	/* UI */
	Rect podPosition = new Rect(10, 10, 96 + 10, 15 + 10);
	Rect previousPosition;
	public int width;
	
	/* Physics */
	public double movementSpeed;
	public long movementStartTime;
	public long movementEndTime;

	//TODO Change to a Point implementation
	public float targetX;


	public Pod(Space space, Context context) {
		BitmapDrawable animation = (BitmapDrawable) context.getResources().getDrawable(R.drawable.arkanoid);
		sprite = new Sprite(animation.getBitmap(), 4, 12, true);
		this.space = space;
		this.movementSpeed = DEFAULT_MOVEMENT_SPEED;
		this.width = 96;
		this.targetX = space.centerX;
	}

	public void updateState(Canvas sky, long elapsedTime) {
		previousPosition = new Rect(podPosition);
		previousPosition.left-=64;
		previousPosition.right+=64;
		//TODO Replantearse la estrategia de escritura de gráficos
		//sky.drawRect(previousPosition, new Paint());

		calculateNewPosition(elapsedTime);
		sprite.nextFrame(elapsedTime);
		sprite.draw(sky, podPosition);

	}

	//FIXME Fix movement on player's pod, not accurate. 'now' should be set in the View...
	private void calculateNewPosition(long elapsedTime) {
		
		long now = System.currentTimeMillis();
		double elapsedMovementTime = Math.max(0, Math.min(now, movementEndTime) - movementStartTime);
		movementStartTime = now;
		
		float targetDisplacement = Math.abs(targetX - podPosition.centerX());
		
		double distance = movementSpeed * elapsedMovementTime / 1000;
		int displacement = (int) Math.round(Math.min(distance, targetDisplacement));
	
		if (podPosition.centerX() > targetX) {
			if (podPosition.left - displacement <= 0) {
				podPosition.left = 0;
			} else {
				podPosition.left = podPosition.left - displacement;
			}
			podPosition.right = podPosition.left + width;
		} else if (podPosition.centerX() < targetX){
			if (podPosition.right + displacement >= space.width) {
				podPosition.right = space.width;
				podPosition.left = podPosition.right - width;
			} else {
				podPosition.left = podPosition.left + displacement;
				podPosition.right = podPosition.left + width;
			}
		}
	}
	/*
	private void calculateNewPosition(long elapsedTime) {
		
		long now = System.currentTimeMillis();
		double elapsedMovementTime = Math.min(now, movementEndTime) - movementStartTime;
		movementStartTime = now;

		double distance = movementSpeed * elapsedMovementTime / 1000;
		int displacement = (int) Math.round(distance);

		if (movingLeft) {
			if (position.left - displacement <= 0) {
				position.left = 0;
			} else {
				position.left = position.left - displacement;
			}
			position.right = position.left + width;
		} else if (movingRight){
			if (position.right + displacement >= space.width) {
				position.right = space.width;
				position.left = position.right - width;
			} else {
				position.left = position.left + displacement;
				position.right = position.left + width;
			}
		}
	}
	*/
}
